/**
 * Representing notes
 * @param {Object} selStr
 */
function Note(selStr, noteBtn){
	this.$noteOuter = $(selStr);
	this.$noteBtn = $(noteBtn);
	this.$notes = this.$noteOuter.children(".note");
	this.$operationOuter = this.$noteOuter.children(".noteOperationOuter");

	this.$hider = this.$operationOuter.children(".hideLink");
	this.init();
}

Note.prototype = {
	/**
	 * inititialize notes 
	 */
	init : function(){
		var _this = this;
		this.$operationOuter.hide();
		this.$notes.hide();
		_this.$operationOuter.children(".hideLink").hide();
		
		this.$hider.children(".hide").click(function(){
			var $thisHider = $(this);
			var $thisOperationOuter = $thisHider.parent().parent();
			var $thisNotes = $thisOperationOuter.siblings(".note");
			$thisOperationOuter.hide();
			$thisNotes.slideUp();
			return false;
		});
		

		
		/**
		 * slide down for the first time and slide up for the second time
		 */
		this.$noteBtn.click(function(){
			var thisBtn = this;
			var $thisBtn = $(thisBtn);
			if($thisBtn.text() == "0 notes"){
				return false;
			}
			
			var $thisOperationOuter = _this.$operationOuter.eq(_this.$noteBtn.index($thisBtn));
			var $thisNotes = $thisOperationOuter.siblings(".note");
			var $thisHider = $thisOperationOuter.children(".hideLink");
			
			if($thisOperationOuter.css("display") == "block"){
				$thisOperationOuter.hide();
				$thisNotes.slideUp();
				return false;
			}else{
				$thisOperationOuter.slideDown("slow");
				if ($thisNotes.children().size() == 0) {
					var url = $thisBtn.attr("href");
					var status = $thisOperationOuter.find(".status");
					$.get(url, function(html){
						$thisNotes.append(html);
						status.remove();
					})
				}
				$thisNotes.slideDown("slow");
				$thisHider.slideDown("slow");
				return false;
			}
			});
			
	}
}



function getNewItems($thread){
	var url = "";
	$.get(url, function(html){
		html = '<div class="item"><div class="pic"><a href="#">xxxx</a></div><!--End pic-->				<div class="item-content">					<div class="operation">						<a href="#" class="note-btn">0 notes</a>						<a href="#" class="reblog">reblog</a>						<a href="#" class="favor">favor</a>					</div><!--End operation-->					<p class="author">						<a href="#">author:</a>					</p><!--End author-->					<p class="text-content">						With HP ePrint, customers can print emails, photos, business materials and more directly to any ePrint-enabled HP printer. - Technology giant Hewlett-Packard (HP) announced the launch of the LaserJet Pro 100 color MFP M175nw, the companys most compact multifunction color laser printer. Priced at $349, the printer is aimed at small to medium-size businesses (SMBs) and offers energy savings from built-in features such as HP...					</p><!--End text-content-->					<div class="tag">						<a href="#">#michael</a>					</div>					<div class="clear"></div>					<div class="noteOuter">						<ol class="note" style="display: none;">							<li>								<a href="#"><img align="absmiddle" src="/dorimeme/app/webroot/img/usericon/profile.jpg"></a>								<span class="author"><a href="">somebody: </a>liked this</span>								<a href="#" class="block">block</a>												</li>						</ol>						<div class="noteOperationOuter" style="display: none;">							<div class="status">Loading...</div>							<div class="hideLink" style="display: none;">								<a href="#" class="hide">									Hide notes								</a>							</div>						</div><!-- End noteOperationOuter -->					</div><!-- End noteOuter -->				</div><!--End item-content-->				<div class="clear"></div>			</div>';
		$html = $(html).hide();
		$thread.prepend($html);
		$html.slideDown();
	});
}

function BlogInfo(ulSel){
	this.$ulContainer = $(ulSel);
	this.follower = this.$ulContainer.find("li:eq(1) a");
	this.draft = this.$ulContainer.find("li:eq(2) a");
	this.init();
}

BlogInfo.prototype = {
	init : function(){
		this.follower.click(function(){
			var url = $(this).attr("href");
			$.get(url, function(html){
				$("#thread").empty().html(html);
				var fAndUf = new FollowAndUnfollow(".follow",".unfollow");
				var sideBar = new SideBarBox("#blogInfo .boxContent");
				sideBar.changeCurrent(1);
			});
			return false;
		});
		
		this.draft.click(function(){
			var url = $(this).attr("href");
			$.get(url, function(html){
				$("#thread").empty().html(html);
				var sideBar = new SideBarBox("#blogInfo .boxContent");
				sideBar.changeCurrent(2);
			});
			return false;
		});
	}
}

function Favor(operationOuter){
	this.$operationOuter = $(operationOuter);
	this.$favorBtn = this.$operationOuter.children(":nth-child(3)");
	this.$favorBtn.each(function(index, value){
		var url = $(value).attr("href");
		if($(value).attr("class") == "favored"){
			url = url.replace(/favorPost/, "unfavorPost");
		}else{
			url = url.replace(/unfavorPost/, "favorPost");
		}
		$(value).attr("href", url);
	});
	this.$favorBtn.click(function(){
		var _this = this;
		var url = $(this).attr("href");

		$.get(url, function(){
			$(_this).toggleClass("favor");
			$(_this).toggleClass("favored");
		});
		if($(this).attr("class") == "favor"){
			url = url.replace(/favorPost/, "unfavorPost");
		}else{
			url = url.replace(/unfavorPost/, "favorPost");
		}
		$(this).attr("href", url);
		return false;
	}); 
	
}

$(document).ready(function(){
	var favor = new Favor(".operation");
	var note = new Note(".noteOuter",".note-btn");
	if($.browser.msie&&(($.browser.version == "7.0")||($.browser.version == "8.0"))){
		$tabs = $('#tab').children('li');
		for($i=0;$i<$tabs.length;$i++){
			$a = $tabs.eq($i).children('a');
			if(!($a.attr('class') == 'tabCurrent'))
				$a.css('background-position',-($i*74)+'px 0');
		}
	}
	var blogInfo = new BlogInfo("#blogInfo");
//	$thread = $("#thread");
//	setInterval("getNewItems($thread)", 10000);
	
});
